/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.gosling.cloudspace.tools;

import com.gosling.cloudspace.common.entity.PageInfoNoPage;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;

/**
 * 算法工具类
 *
 * @author lihonglin
 * @date 2022/3/31 11:23
 */
public class AlgorithmicTool {
    private static int MAX = Integer.MAX_VALUE;
    //定义无向图矩阵
    private static int[][] map = new int[][]{
            {0, 1, 6, 2},
            {1, 0, 3, 2},
            {6, 3, 0, 1},
            {2, 2, 1, 0}
            //prim(map, map.length);
    };

    /***
     * 贪心算法
     *
     * @param graph
     * @param n
     */
    public static void prim(int[][] graph, int n) {
        //定义节点名字
        char[] c = new char[]{'A', 'B', 'C', 'D'};
        //到新集合的最小权
        int[] lowcost = new int[n];
        //存取前驱结点
        int[] mid = new int[n];
        //用来存储加入结点的顺序
        List<Character> list = new ArrayList<Character>();
        int i, j, min, minid, sum = 0;
        //初始化辅助数组
        for (i = 1; i < n; i++) {
            lowcost[i] = graph[0][i];
            mid[i] = 0;
        }
        list.add(c[0]);
        //一共需要加入n-1个点
        for (i = 1; i < n; i++) {
            min = MAX;
            minid = 0;
            //每次找到距离集合最近的点
            for (j = 1; j < n; j++) {
                if (lowcost[j] != 0 && lowcost[j] < min) {
                    min = lowcost[j];
                    minid = j;
                }
            }
            if (minid == 0) {
                return;
            }
            list.add(c[minid]);
            lowcost[minid] = 0;
            sum += min;
            System.out.println(c[mid[minid]] + "到" + c[minid] + " 权值：" + min);
            //加入该点后，更新其它点到集合的距离
            for (j = 1; j < n; j++) {
                if (lowcost[j] != 0 && lowcost[j] > graph[minid][j]) {
                    lowcost[j] = graph[minid][j];
                    mid[j] = minid;
                }
            }
            System.out.print("\n");
        }
        System.out.println("sum:" + sum);
    }

    /***
     * 计算脊背宽度
     * @param bw
     * @param bh
     * @param cb
     * @param mn
     * @author: osc_lihonglin
     * @date: 2021/9/6 9:18
     */
    private void countToBackWidth(List<String> bw, List<String> bh, int cb, Map mn) {
        for (int i = 1; i < cb + 1; i++) {
            //每个盒子的脊背宽度
            int size = bw.size();
            for (int m = 1; m < size + 1; m++) {
                if (m == i && i == 1) {
                    String first = bw.get(0);
                    mn.put(i, first);
                } else if (i == cb) {
                    String last = bw.get(size - 1);
                    mn.put(i, last);
                } else {
                    int p = 0;
                    for (int j = 0; j < m; j++) {
                        p += Integer.parseInt(bh.get(j));
                        if (i < p) {
                            if ((j - 1) > 0) {
                                String bh_get_j_string = bh.get(j);
                                int ij = Integer.parseInt(bh_get_j_string);
                                if (i < (p - ij)) {
                                    Object i_obj = mn.get(i);
                                    if (Objects.isNull(i_obj)) {
                                        mn.put(i, bw.get(j - 1));
                                    }
                                }
                            }
                        } else if (i >= p) {
                            if ((j + 1) < m) {
                                if (i > p) {
                                    String s_j = bh.get(j + 1);
                                    int i_s_j = Integer.parseInt(s_j);
                                    if (i < (i_s_j + p)) {
                                        if (Objects.isNull(mn.get(i))) {
                                            String bwj_1 = bw.get(j + 1);
                                            mn.put(i, bwj_1);
                                        }
                                    }
                                }
                            } else if (i == p) {
                                Object _i_obj = mn.get(i);
                                if (Objects.isNull(_i_obj)) {
                                    String _b_w_j_to_string = bw.get(j);
                                    mn.put(i, _b_w_j_to_string);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /***
     * 手动截取分页数据 每页十条
     *
     * @param compTabId
     * @param page
     * @param rows
     * @return
     * @throws Exception
     */
    public Object dataGridBySubject(String compTabId, Integer page, Integer rows) throws Exception {
        List rowsList = new ArrayList();
        int gap = 0;
        int total = 0;
        int totalAll = 0;
        int count = 1000;
        List<Map<String, Object>> listRow = new ArrayList<>();
        listRow.add(new HashMap<>(8));
        PageInfoNoPage pageInfo = new PageInfoNoPage();
        //记录总条数
        totalAll = totalAll + count;
        for (Map<String, Object> subjectWhere : listRow) {
            //当前页的总条数 显示10条
            total = total + count;
            if (total < page * 10) {
                //如果小于 page * 10 则记录还剩余条数 下次就只需要取gap条
                gap = page * 10 - total;
                pageInfo = new PageInfoNoPage();
                rowsList.addAll(pageInfo.getRows());
                continue;
            } else if (total > page * 10) {
                //如果总条数大于page * 10 且当前查询的条数count也大于page * 10 就直接添加
                if (count > page * 10) {
                    pageInfo.getRows().subList(0, count).forEach(rowsList::add);
                } else {
                    //如果当前查询的count小于page * 10 则说明只需要添加上次剩余的条数即可
                    pageInfo.getRows().subList(0, gap).forEach(rowsList::add);
                }
                break;
            } else if (total == page * 10) {
                //如果总条数等于page * 10 且当前查询条数count也等于page * 10 则说明一个查询条件就满足分页
                if (count == page * 10) {
                    rowsList.addAll(pageInfo.getRows());
                } else {
                    //否则就是加起来才到达的page * 10
                    pageInfo.getRows().subList(0, gap).forEach(rowsList::add);
                }
                break;
            }
        }
        //根据以上的数据整合 截取需要的10条数据 不足10条的返回最后几条 返回并分页
        if (CollectionUtils.isNotEmpty(rowsList)) {
            if (page == 1) {
                //8 < 10
                if (rowsList.size() < 10) {
                    rowsList = rowsList.subList(0, rowsList.size());
                } else {
                    //（0，1，2，3....9）
                    rowsList = rowsList.subList(0, 10);
                }
            } else {
                //16 < 20 （10，11，12，13....15）
                if (rowsList.size() < page * 10) {
                    //取(10,16)->为11到16 包含第一个不包含最后一个
                    rowsList = rowsList.subList(10 * (page - 1), rowsList.size());
                } else {
                    //21 > 20
                    rowsList = rowsList.subList(10 * (page - 1), page * 10);
                }
            }
            pageInfo.setRows(rowsList);
            pageInfo.setTotal(totalAll);
        }
        return pageInfo;
    }

    public static LinkedList Rec_Creat(LinkedList linkedList) {
        LinkedList<Object> objects = new LinkedList<>();
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(linkedList)) {
            for (int i = 0; i < linkedList.size(); i++) {
                if ((i + 1) % 2 == 0) {
                    Object obj = linkedList.get(i);
                    objects.add(obj);
                }
            }
        }
        return objects;
    }
}



